package com.wdc.deliveryrobot.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wdc.deliveryrobot.R;
import com.wdc.deliveryrobot.event.Event;
import com.wdc.deliveryrobot.fragment.CruiseFragment;
import com.wdc.deliveryrobot.fragment.DeliveryFragment;
import com.wdc.deliveryrobot.fragment.MainFragment;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DeliveryActivity extends BaseActivity implements View.OnClickListener,MainFragment.OnItemClickListener{

    private TextView textViewOne, textViewTwo, textViewThree, textViewFour;

    private FrameLayout frameLayout;
    private LinearLayout linearLayout;

    private boolean isDrawerVisible = false;

    private LinearLayout settingsDrawer;

    private String sendMessage;

//    private DeliveryFragment deliveryFragment;

    private MainFragment mainFragment;

    private DeliveryFragment deliveryFragment;

    private CruiseFragment cruiseFragment;

    private Handler handler = new Handler(Looper.myLooper());

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String time = selfChassisState.getAgvlocaltime();

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            textViewOne.setText(selfChassisState.getQuantity() + "%");
                            textViewThree.setText(selfChassisState.getMapName().replace(".yaml", ""));
                            if (!time.equals("")){
                                textViewTwo.setText(formatMillis(Long.valueOf(time)));
                            }

                            if (selfChassisState.isStop()) {
                                textViewFour.setText(getString(R.string.statue_stop));
                            } else {
                                textViewFour.setText(getString(R.string.statue_no));
                            }

                        }
                    });
                    handler.postDelayed(runnable, 1000);

                }
            }).start();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_delivery);

        textViewOne = findViewById(R.id.textOne);
        textViewTwo = findViewById(R.id.textTwo);
        textViewThree = findViewById(R.id.textThree);
        textViewFour = findViewById(R.id.textFour);
        settingsDrawer = findViewById(R.id.settings_drawer);


//
//        sendMessage = buttonSelectMode.getText().toString();
//        deliveryFragment = new DeliveryFragment();
        mainFragment = new MainFragment();
        deliveryFragment = new DeliveryFragment();
        cruiseFragment = new CruiseFragment();
        mainFragment.setListener(this);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, mainFragment)
                    .commit();
        }
        handler.post(runnable);

        Log.d("thd_","数据:" + selfChassisState.getStations());
    }


    @Override
    public void onClick(View view) {
    }


    public void psMessage(String message) {

    }



    public String formatMillis(long millis) {
        String formattedDate = convertTimestampToDate(millis);
        return formattedDate;
    }

    // 将毫秒时间戳转换为完整的年月日时分秒格式
    private String convertTimestampToDate(long timestamp) {
        // 创建一个日期格式化对象，设置为年-月-日 时:分:秒格式
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());

        // 使用日期格式化对象将时间戳转换为日期字符串
        Date date = new Date(timestamp);
        return sdf.format(date);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OnDataEvent(Event.OnDataEvent onDataEvent){
        //Log.d("thd_","数据:" + onDataEvent.charge);


    }

    @Override
    public void checkPag(int page) {
        Log.d("thd_","数据:" + page);
        if (page == 1){
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, deliveryFragment)
                    .addToBackStack(null)
                    .commit();
        }else if (page == 2){
            cruiseFragment.setSelfChassisState(selfChassisState);
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, cruiseFragment)
                    .addToBackStack(null)
                    .commit();
        }else if (page == 3){
            Log.d("thd_","3");
        }else {
            Log.d("thd_","4");
        }
    }


    public void switchToFragment() {
        Log.d("thd_","数据");
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, mainFragment)
                .addToBackStack(null)
                .commit();
    }
}